<?php 
 // header('Content-Type: text/html; charset=cp1251_general_ci');
 header('Content-Type: application/json; charset=utf8_general_ci');
	$arrResult = array();
	
	$query = "SELECT * 
	FROM  `items` 
	WHERE
		status='READY'"; 
	// LIMIT 0 , 30";
	if(isset($_GET['action'])) {
		$types = array();
		$typesQuery = "";
		$limit = "";
		if(isset($_GET['itemsType'])){
			$types = explode(",", $_GET['itemsType']);
		}
		if(isset($_GET['limitAt']) && isset($_GET['limitTo'])){
			$limit = " AND LIMIT {$_GET['limitAt']} , {$_GET['limitTo']}";
		}
		foreach($types as $val) {
			if($typesQuery){
				$typesQuery .= " or ";
			}
			$typesQuery .= "type LIKE '".$val."'";
        }
		if($typesQuery){
			$typesQuery = "and (".$typesQuery.")";
		}
		switch($_GET['action']){
			case 'get_item':
				$query = "SELECT * 
					FROM 
						`items`
					WHERE
						id=".$_GET['item'].
						$limit; 
				break;
			case 'all':
				$query = "SELECT * 
					FROM `items`"; 
				break;
			case 'rental_items':
				$query = "SELECT * 
					FROM  `items` 
					WHERE
						status='RENTAL'
						".$typesQuery."".
						$limit; 
				break;
			case 'gallery_of_finished_products':
				$query = "SELECT * 
					FROM  `items` 
					WHERE
						status='ORDER_COMPLETE'
						".$typesQuery."".
						$limit;
				// echo $query."\n";
				break;
			case 'sell_items':
								$query = "SELECT * 
					FROM  `items` 
					WHERE
						status='SELL'
						AND 
						type='1'".
						$limit; 
				break;
		}
	}
	// echo $query."\n";
	$resultQuerry = mysql_query($query) or die (mysql_error());
	$index = 0;
	while ($row = mysql_fetch_assoc($resultQuerry)) {
		$arrResult[$index++] = $row;
		// var_dump($arrResult[$index]);
		// echo '\n\n';
		// foreach($arrResult[$index] as $key => $value) {
			// $arrResult[$index][$key] = ($value);
		// }
	}
	$index = 0;
	/*foreach($arrResult as $key => $value) {
		// var_dump($value);
		// echo '\n\n';
		foreach($arrResult[$index] as $key1 => $value2) {
			$arrResult[$index][$key1] = utf8_encode($value2);
		}
		// var_dump($arrResult[$index]);
		// echo '\n\n';
		$index++;
	}*/
	//$encodedArray = array_map('utf8_encode', $arrResult);
	// echo json_encode($arrResult, JSON_FORCE_OBJECT )."\n";
	// echo json_encode($arrResult, JSON_UNESCAPED_UNICODE )."\n";
	echo json_encode($arrResult);//."\n";
	//var_dump($arrResult);
	//echo json_last_error();
?>